﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Enums
{
    /// <summary>
    /// 枚举注释类
    /// </summary>
    public static class EnumDescription
    {
        /// <summary>
        /// 获取枚举说明
        /// </summary>
        /// <param name="value">枚举值</param>
        /// <returns>若没有备注说明则返回空</returns>
        public static string GetEnumDescription(this Enum? value)
        {
            if (value == null)
            {
                return "Null";
            }
            try
            {
                var fieldInfo = value.GetType().GetField(value.ToString());
                var descriptionAttributes = fieldInfo?.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];
                if (descriptionAttributes != null && descriptionAttributes.Length > 0)
                {
                    return descriptionAttributes[0].Description;
                }
            }
            catch (Exception) { }
            return "";
        }
    }
}
